﻿using Microsoft.Extensions.Configuration;
using OConf.Entity;
using OConf.Model.Common;
using System;
using System.Linq;
using System.Linq.Expressions;
namespace OConf.Service
{
	public class AdminService
	{
		private readonly IConfiguration _configuration;
		public AdminService(IConfiguration configuration)
		{
			_configuration = configuration;
        }

        public bool IsExists(string id)
        {
            bool isExists = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                isExists = db.Admin.Any(w => w.ID == id);
            }
            return isExists;
        }

        public bool IsExistsByAccountNumber(string accountNumber)
		{
			bool isExists = false;
            using (MyDBContext db = new MyDBContext(_configuration))
			{
                isExists = db.Admin.Any(w => w.AccountNumber == accountNumber);
            }
			return isExists;
		}

		public AdminsEntity FindEntity(string id)
		{
			AdminsEntity entity = null;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                entity = db.Admin.Where(w => w.ID == id).FirstOrDefault();
            }
			return entity;
		}

        /// <summary>
        /// 根据账号获取用户信息
        /// </summary>
        /// <param name="userAccount"></param>
        /// <returns></returns>
        public AdminsEntity GetUserByAccount(string userAccount)
        {
            AdminsEntity entity = null;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                entity = db.Admin.Where(w => w.AccountNumber == userAccount).FirstOrDefault();
            }
            return entity;
        }

        public bool AddEntity(AdminsEntity entity)
		{
			bool isSuccess = false;
            using (MyDBContext db = new MyDBContext(_configuration))
			{
                db.Admin.Add(entity);
                isSuccess = db.SaveChanges() > 0;
            }
			return isSuccess;
        }

		public bool Delete(string id)
		{
            bool isSuccess = false;
            using (MyDBContext db = new MyDBContext(_configuration))
			{
				db.Admin.Remove(new AdminsEntity() { ID = id});
				isSuccess = db.SaveChanges() > 0;
			}
			return isSuccess;
		}

		/// <summary>
		/// 个人中心修改信息
		/// </summary>
		/// <param name="user"></param>
		/// <returns></returns>
		public void UpdatePersonalInfo(AdminsEntity user)
		{
            using (MyDBContext db = new MyDBContext(_configuration))
			{
				db.Admin.Attach(user);
				db.Entry(user).Property(p => p.UserName).IsModified = true;
                db.Entry(user).Property(p => p.Email).IsModified = true;
				db.SaveChanges();
            }
		}

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pwd"></param>
        public void UpdateUserPwd(string id, string pwd)
		{
            using (MyDBContext db = new MyDBContext(_configuration))
			{
				AdminsEntity entity = new AdminsEntity()
				{
					ID = id,
					Password = pwd
				};
				db.Admin.Attach(entity);
				db.Entry(entity).Property(p => p.Password).IsModified = true;
				db.SaveChanges();
			}
		}

		/// <summary>
		/// 修改管理员状态
		/// </summary>
		/// <param name="id"></param>
		/// <param name="enable"></param>
		public void ModifyUserEnable(string id, bool enable)
		{
            using (MyDBContext db = new MyDBContext(_configuration))
			{
				AdminsEntity entity = new AdminsEntity()
				{
					ID = id,
					Enable = enable
				};
				db.Admin.Attach(entity);
				db.Entry(entity).Property(p => p.Enable).IsModified = true;
				db.SaveChanges();
			}
		}

		/// <summary>
		/// 管理员列表编辑用户基本信息
		/// </summary>
		/// <param name="entity"></param>
		public void EditUserInfo(AdminsEntity entity)
		{
            using (MyDBContext db = new MyDBContext(_configuration))
			{
				db.Admin.Attach(entity);
				db.Entry(entity).Property(p => p.UserName).IsModified = true;
                db.Entry(entity).Property(p => p.Email).IsModified = true;
                db.Entry(entity).Property(p => p.RoleType).IsModified = true;
				db.SaveChanges();
            }
		}

		public ReturnTableData<AdminsEntity> QueryPage(int pageIndex, int pageSize, Expression<Func<AdminsEntity, bool>> whereLambda)
		{
			ReturnTableData<AdminsEntity> result = new ReturnTableData<AdminsEntity>();
            using (MyDBContext db = new MyDBContext(_configuration))
			{
				var query = db.Admin.Where(whereLambda);

                result.Total = query.Count();
				if (result.Total > 0)
				{
                    result.List = query.OrderByDescending(o => o.Createtime)
						.Skip(pageSize * (pageIndex - 1))
                        .Take(pageSize).ToList();
                }
            }
			return result;
		}
	}
}
